package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.lishe.lishe.R;

/**
 * Created by DELL on 2017/10/30.
 */

public class ShowBottomDialog extends Dialog {


    private ShowBottomDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Activity context;
        private ShowBottomDialog dialog;
        private int style = R.style.MyTransparentDialog;
        private View contentView;

        public Builder(Activity context) {
            this.context = context;
        }

        public Builder setStyle(int style) {
            this.style = style;
            return this;
        }

        public Builder setContentView(View contentView) {
            this.contentView = contentView;
            return this;
        }

        public ShowBottomDialog create() throws NullPointerException {
            if (dialog == null) {
                dialog = new ShowBottomDialog(context, style);
                dialog.setContentView(contentView);
                dialog.getWindow().setGravity(Gravity.CENTER);
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
            }

            contentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dissmiss();
                }
            });
            return dialog;
        }

        public void show() {
            try {
                create();
                if(dialog!=null&&!dialog.isShowing()){
                    dialog.show();
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        public void dissmiss() {
            if (dialog != null) {
                dialog.dismiss();
                dialog.cancel();
            }
        }

    }

}
